package com.prs.crm.action.hr;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;

import com.prs.crm.domain.hr.SalaryItem;
import com.prs.crm.service.hr.SalaryItemService;

public class SalaryItemAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private SalaryItem salaryItem;
	private SalaryItemService salaryItemService;
	private PaginationSupport pagination;
	private Integer[] salaryItemIds;

	@Action("listSalaryItem")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SalaryItem.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSalaryItemService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editSalaryItem")
	public String edit() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveSalaryItem", results = {
			@Result(name = "success", type = "redirect", location = "listSalaryItem"),
			@Result(name = "error", type = "chain", location = "editSalaryItem") })
	public String save() {
		if (salaryItem.getName() == null
				|| salaryItem.getName().trim().equals("")) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (salaryItem.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		this.setPrompt("提交薪酬项目成功");
		this.getSalaryItemService().save(salaryItem);
		return SUCCESS;
	}

	@Action(value = "removeSalaryItem", results = {
			@Result(name = "success", type = "redirect", location = "listSalaryItem"),
			@Result(name = "error", type = "chain", location = "listSalaryItem") })
	public String remove() {
		if (salaryItemIds == null) {
			this.addActionError("请选择要删除的标准项目");
			return ERROR;
		}
		for (Integer id : salaryItemIds) {
			SalaryItem salaryItem = this.getSalaryItemService().get(id);
			if (salaryItem.getRelats().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getSalaryItemService().batchRemove(this.getSalaryItemIds());
		return SUCCESS;
	}

	public SalaryItem getSalaryItem() {
		return salaryItem;
	}

	public void setSalaryItem(SalaryItem salaryItem) {
		this.salaryItem = salaryItem;
	}

	public SalaryItemService getSalaryItemService() {
		return salaryItemService;
	}

	public void setSalaryItemService(SalaryItemService salaryItemService) {
		this.salaryItemService = salaryItemService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getSalaryItemIds() {
		return salaryItemIds;
	}

	public void setSalaryItemIds(Integer[] salaryItemIds) {
		this.salaryItemIds = salaryItemIds;
	}

}
